package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**
 * 症状列表
 * @author Administrator
 *
 */
public class Health2 extends Activity
{

	private TextView health2_bt; // 标题
	private ListView health2_lv; // 症状明细
	private TextView health2_id; // 部位id
	private Intent mIntent;
	private ImageButton backButton;

	private Health2 oThis = this;
	private CustomProgressDialog proDialog;// 加载框
	private Map<String, Object> map_zz; // 人体症状map
	List<Map<String, Object>> list_zz = new ArrayList<Map<String, Object>>();// 人体症状list
	private MyAdapter myAdapter;
	private int preId = -1;
	private String sex;
	private String partname;

	private ImageButton mainselected;
	MyApplication myApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.health2_main);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		// 初始化view
		findView();

		// 初始化界面数据
		GetData();

		// 点击事件
		setOnClick();
	}

	// 初始化view
	private void findView()
	{
		health2_lv = (ListView) this.findViewById(R.id.health2_lv);
		health2_bt = (TextView) this.findViewById(R.id.health2_bt);
		health2_id = (TextView) this.findViewById(R.id.health2_id);
		backButton = (ImageButton) this.findViewById(R.id.backButton);
		mIntent = getIntent();
		if (mIntent != null)
		{
			partname = mIntent.getStringExtra("partname");
			sex = mIntent.getStringExtra("sex");
			health2_bt.setText(partname + "症状");
			health2_id.setText(sex);//
		}
		mainselected = (ImageButton) this.findViewById(R.id.mainselected);
	}

	// 初始化界面数据
	private void GetData()
	{
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();

	}

	class LoadData implements Runnable
	{

		public void run()
		{
			// TODO Auto-generated method stub
			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			ls.add(new BasicNameValuePair("sex", sex));
			ls.add(new BasicNameValuePair("partname", partname));
			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.HEALTH, ls);
			List<Map<String, Object>> tempmain = new ArrayList<Map<String, Object>>();
			if (json != null)
			{
				if (!json.toString().equals("{\"msg\":\"系统错误！\",\"success\":\"false\"}"))
				{
					if (!json.getString("orglist").equals(""))
					{
						JSONArray jsonArr = new JSONArray(json.getString("orglist"));
						// JSONArray jsonArr = json.getJSONArray("userjsonstr");
						for (int i = 0; i < jsonArr.length(); i++)
						{
							JSONObject js = jsonArr.getJSONObject(i);
							map_zz = new HashMap<String, Object>();
							map_zz.put("id", js.getString("id"));
							map_zz.put("mc", js.getString("lbmc"));
							tempmain.add(map_zz);
						}
						msg.what = 0;
						msg.obj = tempmain;
					}
					else
					{
						msg.what = -1;
						msg.obj = "暂无数据";
					}
				}
				else
				{
					msg.what = -1;
					msg.obj = "系统错误";
				}
			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接失败!";
			}
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			Message msg = myHandler.obtainMessage();
			msg.what = -1;
			msg.obj = e.toString();
			myHandler.sendMessage(msg);
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					proDialog.dismiss();
					Toast.makeText(oThis, (String) msg.obj, Toast.LENGTH_SHORT).show();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					list_zz = (List<Map<String, Object>>) msg.obj;
					if (myAdapter == null)
					{
						myAdapter = new MyAdapter(oThis);
					}

					myAdapter.setList(list_zz);
					health2_lv.setAdapter(myAdapter);
					myAdapter.notifyDataSetChanged();
					break;
				case 1:
					break;

				default:
					break;
			}
		};
	};

	private void setData()
	{
		myAdapter = new MyAdapter(this);
		myAdapter.setList(list_zz);
		health2_lv.setAdapter(myAdapter);
	}

	class MyAdapter extends BaseAdapter
	{
		private List<Map<String, Object>> list;
		private LayoutInflater mLayoutInflater;

		public MyAdapter(Context context)
		{
			mLayoutInflater = LayoutInflater.from(context);
		}

		public List<Map<String, Object>> getList()
		{
			return list;
		}

		public void setList(List<Map<String, Object>> list)
		{
			this.list = list;
		}

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (convertView == null)
			{
				convertView = mLayoutInflater.inflate(R.layout.health2_sub, null);
				holder = new ViewHolder();
				holder.health2_sub_mc = (TextView) convertView.findViewById(R.id.health2_sub_mc);
				holder.health2_sub_id = (TextView) convertView.findViewById(R.id.health2_sub_id);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.health2_sub_mc.setText((CharSequence) list.get(position).get("mc"));
			holder.health2_sub_id.setText((CharSequence) list.get(position).get("id"));

			if (position % 2 == 0)
			{// 偶数
				if (position == preId)
					convertView.setBackgroundColor(getResources().getColor(R.color.listhead_color));
				else
					convertView.setBackgroundColor(getResources().getColor(R.color.backgroundMYD));
			}
			else
			{// 奇数
				if (position == preId)// 选中
					convertView.setBackgroundColor(getResources().getColor(R.color.listhead_color));
				else
					convertView.setBackgroundColor(getResources().getColor(R.color.white_bg));
			}
			return convertView;
		}
	}

	public class ViewHolder
	{
		private TextView health2_sub_mc;
		private TextView health2_sub_id;
	}

	// 点击事件
	private void setOnClick()
	{

		backButton.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub

				Health2.this.onBackPressed();

			}
		});
		health2_lv.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				// TODO Auto-generated method stub
				myAdapter.notifyDataSetChanged();

				TextView health1_sub_mc = (TextView) view.findViewById(R.id.health2_sub_mc);
				TextView health1_sub_id = (TextView) view.findViewById(R.id.health2_sub_id);
				String strMc = health1_sub_mc.getText().toString();
				String strId = health1_sub_id.getText().toString();
				Intent intent = new Intent(Health2.this, Health3.class);
				intent.putExtra("mc", strMc);
				intent.putExtra("id", strId);
				intent.putExtra("type", "Health2");
				startActivity(intent);
				preId = position;

			}
		});
		mainselected.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(oThis, Mainmean.class);
				startActivity(intent);
				oThis.finish();
			}
		});

	}

}
